/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import org.apache.log4j.Logger;

/**
 * A dialog to edit a String on several lines
 * @author stl
 *
 */
public class JDialogEditMultiLineString extends JDialog {

    private static Logger logger = Logger.getLogger(JDialogEditMultiLineString.class);	
	
	private JPanel jContentPane = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	
	private boolean okPressed =false;
	private String inputStr;

	private JScrollPane jScrollPaneTitle = null;

	private JTextPane jTextPaneTitle = null;
	
	/**
	 * This is the default constructor
	 */
	public JDialogEditMultiLineString( Frame parent, boolean modal, String title, String inputStr ) {
		super( parent, modal );
		
		this.setTitle( title );		
		this.inputStr =inputStr;
		
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		//this.setTitle("Edit Title");

		this.setBounds(new Rectangle(0, 0, 400,200));
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJScrollPaneTitle(), BorderLayout.CENTER);
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");

			jButtonOK.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {
					okPressed =true;
					inputStr =getJTextPaneTitle().getText();
					setVisible( false );
				}
			});
		}
		return jButtonOK;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {
					setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}

	public boolean isOkPressed() {
		return okPressed;
	}
	
	/**
	 * This method initializes jScrollPaneTitle	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneTitle() {
		if (jScrollPaneTitle == null) {
			jScrollPaneTitle = new JScrollPane();
			jScrollPaneTitle.setViewportView(getJTextPaneTitle());
		}
		return jScrollPaneTitle;
	}
	/**
	 * This method initializes jTextPaneTitle	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getJTextPaneTitle() {
		if (jTextPaneTitle == null) {
			jTextPaneTitle = new JTextPane();
			jTextPaneTitle.setText( inputStr != null ? inputStr : "" );
		}
		return jTextPaneTitle;
	}
	
	public String getInputStr() {
		return inputStr;
	}
	
	public void setInputStr(String inputStr) {
		this.inputStr = inputStr;
	}	
	
 }  
